package bitcarve.hydra.ui.timeline
{
	import bitcarve.hydra.core.event.TrackEvent;
	import bitcarve.hydra.core.time.TimePoint;
	import bitcarve.hydra.core.track.core.ITrack;
	import bitcarve.hydra.ui.assets.RectMask;
	import bitcarve.hydra.ui.control.BasePanel;
	import bitcarve.hydra.ui.timeline.layout.*;
	import bitcarve.hydra.ui.timeline.viewer.BackgroundViewer;
	import bitcarve.hydra.ui.timeline.viewer.GridViewer;
	import bitcarve.hydra.ui.timeline.viewer.TrackViewer;
	
	import flash.display.Sprite;
	
	public class TimelinePanel extends BasePanel implements ITimelinePanel
	{
		//Timeline Track Data
		private var track:ITrack;
		
		//Navigator
		private var navigator:TimelineNavigator;
		
		//Layouts
		private var backgroundLayout:IBackgroundLayout;
		private var gridLayout:IGridLayout;
		private var trackLayout:ITrackLayout;
		
		//Viewers
		private var backgroundViewer:BackgroundViewer;
		private var trackViewer:TrackViewer;
		private var gridViewer:GridViewer;
		
		public var content:Sprite;
		
		
		public function TimelinePanel()
		{
			super(1000, 500);
			this.x = 20;
			this.y = 150;
			
			init();
		}
		
		public function init():void
		{
			backgroundLayout = new BackgroundLayout();	
			backgroundViewer = new BackgroundViewer();
			
			gridViewer = new GridViewer();
			gridLayout = new GridLayout();
						
			trackLayout = new BasicTrackLayout();
			trackViewer = new TrackViewer();
			
			trackLayout.startTime = new TimePoint(0);
			trackLayout.endTime = new TimePoint(200);
			gridLayout.startTime = new TimePoint(0);
			gridLayout.endTime = new TimePoint(200);
			backgroundLayout.startTime = new TimePoint(0);
			backgroundLayout.endTime = new TimePoint(200);
			
			
			trackLayout.addEventListener(LayoutUpdateEvent.UPDATE, backgroundLayout.update);		
			trackLayout.addEventListener(LayoutUpdateEvent.UPDATE, gridLayout.update);
			trackLayout.addEventListener(LayoutUpdateEvent.UPDATE, trackViewer.update);
			backgroundLayout.addEventListener(LayoutUpdateEvent.UPDATE, backgroundViewer.update);
			gridLayout.addEventListener(LayoutUpdateEvent.UPDATE, gridViewer.update);

			content = new Sprite();
			addChild(content);
			content.addChild(backgroundViewer);
			content.addChild(gridViewer);
			content.addChild(trackViewer);
		}
		
		public function get startTime() : TimePoint
		{
			return null;
		}
		
		public function set startTime( value : TimePoint ):void
		{
			
		}
		
		public function get endTime() : TimePoint
		{
			return null;
		}
		
		public function set endTime( value : TimePoint ):void
		{
			
		}	
		
		public function get trackProvider():ITrack
		{
			return this.track;
		}
		
		public function set trackProvider( value : ITrack ):void
		{
			if(track)
			{
				track.removeEventListener(TrackEvent.TRACK_MODELCHANGE, modelChangeHandler);
			}
			track = value;
			trackLayout.trackProvider = track;
			track.addEventListener(TrackEvent.TRACK_MODELCHANGE, modelChangeHandler);
			
			var event:TrackEvent = new TrackEvent(TrackEvent.TRACK_MODELCHANGE);
			dispatchEvent(event);
			modelChangeHandler(event);
		}
		
		protected function modelChangeHandler(event:TrackEvent):void
		{
			trackLayout.updateModel(event);
			trackLayout.update();
		}
				
		
	}
}